import { SysWsUrl } from '../db/models/sys_ws_url'

export class WsManager {
  private static instance: WsManager

  constructor() {
    // this._sysUser = []
    // this._id = id
    // this._loadFromDB()
  }


  public static getInstance(): WsManager {
    if (!WsManager.instance) {
      WsManager.instance = new WsManager()
    }
    return WsManager.instance
  }

  //根据参数修改数据
  async update(id: number, host: string, port: string) {
    return await SysWsUrl.update(
      {
        host: host,
        port: port
      },
      {
        where: {
          id: id
        }
      }
    )
  }

  //根据参数添加数据
  async addWsUrl(host: string, port: string) {
    return await SysWsUrl.create({
      host: host,
      port: port
    })
  }

  //根据id删除数据
  async removeWsUrl(dId: string) {
    await SysWsUrl.destroy({
      where: {
        id: dId
      }
    }).then((value: number) => {
      if (value > 0) {
        // this._loadFromDB()
      }
    })
  }

  //查询全部数据
  async getAll() {
    return await SysWsUrl.findAll({}).then((value: Array<SysWsUrl>) => {
      return JSON.stringify(value)
    })
  }

  //根据用户名称查询数据
  async getByName(host: string) {
    return await SysWsUrl.findAll({
      where: {
        host: host
      }
    }).then((value: Array<SysWsUrl>) => {
      return JSON.stringify(value)
    })
  }
}
